package com.dtagri.fjwlw.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.sax.StartElementListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.adapter.viewholders.VideoCenterViewHolder;
import com.dtagri.fjwlw.bean.TjNvr;
import com.dtagri.fjwlw.bean.TjNvrChanel;
import com.dtagri.fjwlw.bean.tdvideo.VideoInfo;
import com.dtagri.fjwlw.fragment.VideoCenterFragment;
import com.dtagri.fjwlw.ui.VideoRegister;
import com.dtagri.fjwlw.util.TToast;

/**
 * 视频中心适配器
 * @author wxjn
 * 
 */
public class VideoCenterAdapter extends
		CommonAdapter<TjNvr, VideoCenterViewHolder> {

	private VideoCenterFragment parentFragment;
	private Context context;
	private Intent intent;
	public VideoCenterAdapter(Context context, VideoCenterFragment parent,
			List<TjNvr> mDataSet) {
		super(context, mDataSet);
		this.parentFragment = parent;
		this.context=context;
	}

	@Override
	public int getCount() {
		int i = 0;
		for (TjNvr tjNvr : mDataSet) {
			List<TjNvrChanel> tjNvrChanels = tjNvr.getTjNvrChanels();
			i += tjNvrChanels.size();
		}
		return i;
	}

	@Override
	protected VideoCenterViewHolder createViewHolder() {
		return new VideoCenterViewHolder();
	}

	@Override
	protected void setItemData(int position, VideoCenterViewHolder viewHolder,
			View rootView) {
		// 遍历得到当前通道
		int i = 0;
		for (final TjNvr tjNvr : mDataSet) {
			int j = 0;
			for (final TjNvrChanel tjNvrChanel : tjNvr.getTjNvrChanels()) {
				if (i == position) {
					viewHolder.channel.setText(tjNvrChanel.getSsaddr());
					viewHolder.nvr.setText(tjNvr.getNvraddr());
					if(tjNvrChanel.getType()==0){
						Drawable drawableBall = mContext.getResources().getDrawable(R.drawable.video_ball);
						viewHolder.video.setBackgroundDrawable(drawableBall);
						
					}else{
						Drawable drawableGun = mContext.getResources().getDrawable(R.drawable.video_gun);
						viewHolder.video.setBackgroundDrawable(drawableGun);
						
					}
					
					if (j == 0) {
						viewHolder.titleLine.setVisibility(View.VISIBLE);
					} else {
						viewHolder.titleLine.setVisibility(View.GONE);
					}
					viewHolder.video.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							//查看视频播放CameraFlag   1=海康 0=天地伟业
							//调用海康摄像头
							if(tjNvr.getCameraFlag().equals("1")){
//								TToast.showToast("类型："+tjNvr.getCameraFlag()+"-->海康摄像头");
//								tjNvr.setAppDataPort(String.valueOf(Integer.parseInt(tjNvr.getAppDataPort())-1));
								parentFragment.changeCamera(tjNvrChanel,tjNvr);
							}
							//调用天地伟业摄像头
							else if(tjNvr.getCameraFlag().equals("0")){
//								TToast.showToast("类型："+tjNvr.getCameraFlag()+"-->天地伟业摄像头");
//								//设置天地伟业视频播放需要的信息
//								System.out.println(tjNvr.getNvrusername()+tjNvr.getNvruserpwd()+"--------帐号密码--");
//								System.out.println(tjNvrChanel.getSschanelnum()+"----------频道号");
//								System.out.println(tjNvr.getNvrwanip()+"IP!!!");
//								System.out.println(tjNvrChanel.getSsstream()+"------流号");
//								System.out.println(tjNvrChanel.getSsstep()+"-------步");
//								System.out.println(tjNvrChanel.getSsdevaddr()+"------addr");
//								System.out.println(tjNvr.getNvrdataport()+"-----端口号");
								
								
//								VideoInfo vInfo=new VideoInfo();
//								vInfo.setVideoPort(3000);
								//通道号要+1
								
//								vInfo.setVideoChannel(1);
//								vInfo.setVideoUsername("admin");
//								vInfo.setVideoPwd("1111");
//								vInfo.setVideoIp("110.90.20.41");
//								vInfo.setHostID(11);
//								vInfo.setVideoBaud(0);
//								vInfo.setVideoComNum(6);
//								vInfo.setVideoAddress(-1);
//								vInfo.setVideoStep(0);
//								vInfo.setVideoProtocol(1);
//								vInfo.setVideoStream(0);
		
								parentFragment.change(tjNvr,tjNvrChanel);
								
							}

						}
					});
				}
				i++;
				j++;
			}
		}

	}

}
